import UIKit

class ActivityIndicatorExampleViewController: UIViewController {
    
    // MARK: - 控件属性
    private let activityIndicator = UIActivityIndicatorView(style: .large)
    private let statusLabel = UILabel()
    private let startButton = UIButton(type: .system)
    private let stopButton = UIButton(type: .system)
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.backgroundColor = .systemBackground
        setupUI()
        setupConstraints()
    }
    
    // MARK: - UI 设置
    private func setupUI() {
        // 创建 UIActivityIndicatorView
        activityIndicator.translatesAutoresizingMaskIntoConstraints = false
        activityIndicator.color = .systemBlue  // 设置颜色
        activityIndicator.hidesWhenStopped = true  // 停止时自动隐藏
        view.addSubview(activityIndicator)
        
        // 创建状态标签
        statusLabel.translatesAutoresizingMaskIntoConstraints = false
        statusLabel.font = UIFont.systemFont(ofSize: 18, weight: .medium)
        statusLabel.textAlignment = .center
        statusLabel.text = "准备就绪"
        view.addSubview(statusLabel)
        
        // 创建开始按钮
        startButton.translatesAutoresizingMaskIntoConstraints = false
        startButton.setTitle("开始加载", for: .normal)
        startButton.backgroundColor = .systemBlue
        startButton.tintColor = .white
        startButton.layer.cornerRadius = 8
        startButton.addTarget(self, action: #selector(startLoading), for: .touchUpInside)
        view.addSubview(startButton)
        
        // 创建停止按钮
        stopButton.translatesAutoresizingMaskIntoConstraints = false
        stopButton.setTitle("停止加载", for: .normal)
        stopButton.backgroundColor = .systemRed
        stopButton.tintColor = .white
        stopButton.layer.cornerRadius = 8
        stopButton.addTarget(self, action: #selector(stopLoading), for: .touchUpInside)
        stopButton.isEnabled = false  // 初始禁用
        view.addSubview(stopButton)
    }
    
    // MARK: - 布局约束（修复部分）
    private func setupConstraints() {
        let margin: CGFloat = 20
        NSLayoutConstraint.activate([
            // ActivityIndicator 约束
            activityIndicator.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            activityIndicator.centerYAnchor.constraint(equalTo: view.centerYAnchor),
            
            // 状态标签约束
            statusLabel.centerXAnchor.constraint(equalTo: view.centerXAnchor),
            statusLabel.topAnchor.constraint(equalTo: activityIndicator.bottomAnchor, constant: margin * 2),
            
            // 开始按钮约束
            startButton.leadingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.leadingAnchor, constant: margin * 2),
            startButton.topAnchor.constraint(equalTo: statusLabel.bottomAnchor, constant: margin * 3),
            startButton.heightAnchor.constraint(equalToConstant: 44),
            
            // 停止按钮约束（修复：使用 stopButton.topAnchor 而非 startButton.topAnchor）
            stopButton.trailingAnchor.constraint(equalTo: view.safeAreaLayoutGuide.trailingAnchor, constant: -margin * 2),
            stopButton.topAnchor.constraint(equalTo: statusLabel.bottomAnchor, constant: margin * 3), // 修复此处
            stopButton.heightAnchor.constraint(equalToConstant: 44),
            stopButton.leadingAnchor.constraint(equalTo: startButton.trailingAnchor, constant: margin)
        ])
    }
    
    // MARK: - 交互逻辑（保持不变）
    @objc private func startLoading() {
        // 开始动画
        activityIndicator.startAnimating()
        
        // 更新状态
        statusLabel.text = "加载中..."
        
        // 切换按钮状态
        startButton.isEnabled = false
        startButton.backgroundColor = .systemGray
        stopButton.isEnabled = true
        stopButton.backgroundColor = .systemRed
        
        // 模拟加载过程（3秒后自动停止）
        DispatchQueue.main.asyncAfter(deadline: .now() + 3.0) {
            self.stopLoading()
        }
    }
    
    @objc private func stopLoading() {
        // 停止动画
        activityIndicator.stopAnimating()
        
        // 更新状态
        statusLabel.text = "加载完成"
        
        // 切换按钮状态
        startButton.isEnabled = true
        startButton.backgroundColor = .systemBlue
        stopButton.isEnabled = false
        stopButton.backgroundColor = .systemGray
        
        // 2秒后恢复初始状态
        DispatchQueue.main.asyncAfter(deadline: .now() + 2.0) {
            self.statusLabel.text = "准备就绪"
        }
    }
}
